package com.github.baichuan.gb32960.codec;

import com.github.baichuan.gb32960.utils.ByteBufHelper;
import com.github.baichuan.iot.protocol.pdu.Decoder;
import io.netty.buffer.ByteBuf;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 发动机数据
 */
public class Engine implements Decoder {

    @Override
    public List<Map<String, Object>> decode(ByteBuf buf) {
        Map<String, Object> data = new TreeMap<>();

        // 发动机状态
        data.put("status", buf.readUnsignedByte());

        // 曲轴转速
        ByteBufHelper.readUnsignedShort(buf).ifPresent(value -> {
            data.put("rpm", value);
        });

        // 燃料消耗率
        ByteBufHelper.readUnsignedShort(buf).ifPresent(value -> {
            data.put("fuelConsumption", BigDecimal.valueOf(value * 0.01).setScale(2, RoundingMode.HALF_UP));
        });

        return Arrays.asList(data);
    }
}
